package edu.gatech.oad.user;

// Import Statements
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import edu.gatech.oad.ui.MainPanel;

/**
 * User interface class for logging in. 
 * @author Joey Slater
 * @version 1.00
 */
public class LoginPanel extends JPanel {
	private static final long serialVersionUID = 1L;
	private JTextField userName; 
	private JPasswordField passWord;
	private JButton enter, register;
	private JLabel usrNme, pssWrd, welcome; 
	private Image backgroundImage;
	private static User user = null; 
	private MainPanel mainPanel;
	private Dimension size;
	private GridBagConstraints gbc = new GridBagConstraints();
	/**
	 * Constructor class. Uses 
	 * @param mainPanel main panel used for switching to user panel
	 */
	public LoginPanel(MainPanel mainPanel){
		this.mainPanel = mainPanel;
		setBackground(Color.white); //sets the color for he background  
		this.backgroundImage = 
			(new ImageIcon("src/edu/gatech/oad/images/PrideRock.jpg")).getImage();
		this.size = new Dimension(backgroundImage.getWidth(null), 
			backgroundImage.getHeight(null));
		
		mainPanel.setPreferredSize(size);
		mainPanel.setMaximumSize(size);
		mainPanel.setMinimumSize(size);
		
		this.setLayout(new GridBagLayout()); //sets the layout
		this.welcome = new JLabel("Please Sign In", JLabel.CENTER); 
		this.usrNme = new JLabel("Enter Username: ", JLabel.CENTER); 
		this.userName = new JTextField(12); 
		userName.addActionListener(new ButtonListener()); //adds the username action listener 
		userName.setActionCommand("Enter");
		this.pssWrd = new JLabel("Enter Password: ", JLabel.CENTER); //adds the password label 
		this.passWord = new JPasswordField(12); //the new password field 
		passWord.addActionListener(new ButtonListener()); //add the action listener 
		passWord.setActionCommand("Enter");

		this.enter = new JButton("Enter"); //new button 
		enter.addActionListener(new ButtonListener());
		enter.setActionCommand("Enter");
		this.register = new  JButton("Register");
		register.addActionListener(new ButtonListener());
		register.setActionCommand("Register");
	
		editUI();
		addToPanel();
	}
	
	public Dimension getLoginPanelDimension(){
		return size;
	}
	
	private void editUI(){
		welcome.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 20));
		welcome.setHorizontalAlignment(JLabel.CENTER);
		welcome.setHorizontalTextPosition(JLabel.CENTER);
		enter.setBackground(Color.white); //sets the color 
		enter.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 16));
		register.setBackground(Color.white); //sets the color 
		register.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 16));
	}
	
	private void addToPanel(){
		gbc.anchor = GridBagConstraints.FIRST_LINE_START;
		//gbc.fill = GridBagConstraints.BOTH;
		gbc.insets = new Insets(0,20,5,5);
		gbc.gridwidth = 2;
		gbc.ipadx = 150;
		add(welcome, gbc);
		gbc.ipadx = 0;
		gbc.gridwidth = 1;
		gbc.gridy = 1;
		add(usrNme, gbc);
		gbc.gridx = 1;
		add(userName, gbc);
		gbc.gridy = 2;
		add(passWord, gbc);
		gbc.gridx = 0;
		add(pssWrd, gbc);
		gbc.gridy = 3;
		gbc.gridwidth = 2;
		add(enter, gbc);
		gbc.gridx = 1;
		gbc.weighty = 1;
		gbc.weightx = 1;
		add(register, gbc);
	}
	
	private class ButtonListener implements ActionListener{
		public void actionPerformed(ActionEvent e) {
			if(e.getActionCommand().equals("Enter")){
				LoginController loginController = new LoginController(); //calls the login controller 
				user = loginController.login(userName.getText(), new String(passWord.getPassword()));
				if(user == null){
					welcome.setText("Wrong Username/Password \n Please Try Again");
					welcome.setForeground(Color.red);
					passWord.setText("");
					if(AccountsController.sharedController().getUserForUsername(userName.getText()) != null){
						if(AccountsController.sharedController().getUserForUsername(userName.getText()).isSuspended()){
							welcome.setText("You are Suspended, Please contact System Admin");
						}
					}
				}
				else{
					welcome.setText("Please Sign In");
					welcome.setForeground(Color.black);
					mainPanel.switchToUserPanel(user);
					userName.setText("");
					passWord.setText("");
				}
			}
			if(e.getActionCommand().equals("Register")){
					welcome.setText("Please Sign In");
					welcome.setForeground(Color.black);
					mainPanel.switchToRegisterPanel();
					userName.setText("");
					passWord.setText("");
			}
		}
	}
	
	public void paintComponent(Graphics g){
		g.drawImage(backgroundImage, 0,0, null);
	}
}
